package com.sskj.mine.ui.activity;


import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lzy.okgo.OkGo;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.BindMobileActivityPresenter;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


@Route(path = RConfig.MINE_BIND_MOBILE)//绑定手机号
public class BindMobileActivity extends BaseActivity<BindMobileActivityPresenter> {
    @BindView(R2.id.etNumber)
    EditText etNumber;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.btSendCode)
    TextView btSendCode;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private Disposable disposable;
    @Inject
    UserViewModel userViewModel;
    private boolean isSuccess = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_bind_mobile;
    }

    @Override
    public BindMobileActivityPresenter getPresenter() {
        return new BindMobileActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mineBindMobileActivity0));
        DaggerUserDataComponent.create().inject(this);
        ClickUtil.click(btSubmit, () -> {
            String number = etNumber.getText().toString().trim();
            String code = etCode.getText().toString().trim();
            if (!CheckUtil.isMobile(number)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineBindMobileActivity1));
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineBindMobileActivity2));
                return;
            }
            mPresenter.bindMobile(number, code);
        });
        ClickUtil.click(btSendCode, () -> {
            String number = etNumber.getText().toString().trim();
            if (CheckUtil.isMobile(number)) {
                btSendCode.setEnabled(false);
                mPresenter.sendCode(number);
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mineBindMobileActivity1));
            }
        });
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0 && isSuccess) {
                OkGo.getInstance().getCommonHeaders().put(SPConfig.ACCOUNT, users.get(0).getAccount());
                OkGo.getInstance().getCommonParams().put(SPConfig.ACCOUNT, users.get(0).getAccount());
                OkGo.getInstance().getCommonHeaders().put(SPConfig.MOBILE, users.get(0).getTel());
                OkGo.getInstance().getCommonParams().put(SPConfig.MOBILE, users.get(0).getTel());
            }
        });

    }


    public void onResponseSuccess() {
        isSuccess = true;
        userViewModel.update();
        finish();
    }

    public void sendCodeOk() {
        DisposUtil.close(disposable);
        disposable = Flowable.intervalRange(1, 60, 0, 1, TimeUnit.SECONDS)
                .map(i -> 60 - i)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(i -> {
                    btSendCode.setText(String.format(App.INSTANCE.getString(R.string.mineBindMobileActivity4), i));
                }, System.out::println, () -> {
                    btSendCode.setEnabled(true);
                    btSendCode.setText(App.INSTANCE.getString(R.string.mineBindMobileActivity5));
                });

    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(disposable);
        super.onDestroy();
    }

    public void sendCodeError() {
        btSendCode.setEnabled(true);
    }

    @Override
    public boolean getImmersion() {
        return false;
    }
}
